unit gui_form_Tiatai_Field;

{
  Displays the field how the AI sees it
}

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, ExtCtrls,
  SystemManager, StateMachine, basicio_city, basicio_field, fileprovider,
  basicio_street, basicio_player;

type

  { TTTiatai_FieldForm }

  TTTiatai_FieldForm = class(TForm)
    F_2_1: TImage;
    F_3_1: TImage;
    F_5_1: TImage;
    F_7_1: TImage;
    F_8_1: TImage;
    F_7_2: TImage;
    F_8_2: TImage;
    F_3_3: TImage;
    F_5_3: TImage;
    F_7_3: TImage;
    F_9_1: TImage;
    F_1_1: TImage;
    F_2_2: TImage;
    F_4_1: TImage;
    F_3_2: TImage;
    F_4_2: TImage;
    F_6_1: TImage;
    F_5_2: TImage;
    F_6_2: TImage;
    C_1_3: TImage;
    C_2_5: TImage;
    C_3_3: TImage;
    C_3_4: TImage;
    C_3_5: TImage;
    C_3_1: TImage;
    C_4_1: TImage;
    C_4_2: TImage;
    C_4_3: TImage;
    C_4_4: TImage;
    C_2_3: TImage;
    C_5_2: TImage;
    C_5_3: TImage;
    C_6_3: TImage;
    C_2_7: TImage;
    C_2_6: TImage;
    C_1_7: TImage;
    C_1_6: TImage;
    C_2_8: TImage;
    C_2_9: TImage;
    C_1_8: TImage;
    C_1_9: TImage;
    C_2_10: TImage;
    C_3_6: TImage;
    C_3_2: TImage;
    C_3_8: TImage;
    C_3_9: TImage;
    C_3_10: TImage;
    C_3_7: TImage;
    C_4_7: TImage;
    C_2_2: TImage;
    C_4_5: TImage;
    C_4_6: TImage;
    C_4_8: TImage;
    C_4_9: TImage;
    C_4_10: TImage;
    C_3_11: TImage;
    C_4_11: TImage;
    C_5_4: TImage;
    C_5_5: TImage;
    C_6_5: TImage;
    C_6_4: TImage;
    C_5_6: TImage;
    C_6_6: TImage;
    C_6_7: TImage;
    C_5_7: TImage;
    C_5_8: TImage;
    C_6_8: TImage;
    C_2_4: TImage;
    C_6_9: TImage;
    C_5_9: TImage;
    C_5_10: TImage;
    C_1_4: TImage;
    C_1_5: TImage;
    S_All: TImage;
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
    FPic_RK_Wood   : TBitmap;
    FPic_RK_Dirt   : TBitmap;
    FPic_RK_Sheeps : TBitmap;
    FPic_RK_Wheat  : TBitmap;
    FPic_RK_Ore    : TBitmap;
    FPic_PK_None   : TBitmap;
  protected
    procedure UpdateFieldImage(AImage: TImage; AField: TField; AHasThief: boolean=false);
    procedure UpdateCities;
    procedure UpdateStreets;
    procedure UpdatePlayField;
    procedure UpdateHarbors;
  public
    procedure UpdateInfos;
  end;

implementation

{$R *.lfm}

{ TTTiatai_FieldForm }

procedure TTTiatai_FieldForm.FormCreate(Sender: TObject);
var
  Path: String;
begin
  path := GlobalSystemManager.FileProvider.ProvideFile('FieldPics\', FT_Data);

  FPic_RK_Wood   := TBitmap.Create; FPic_RK_Wood.LoadFromFile(path+'RK_Wood.bmp');
  FPic_RK_Dirt   := TBitmap.Create; FPic_RK_Dirt.LoadFromFile(path+'RK_Dirt.bmp');
  FPic_RK_Sheeps := TBitmap.Create; FPic_RK_Sheeps.LoadFromFile(path+'RK_Sheeps.bmp');
  FPic_RK_Wheat  := TBitmap.Create; FPic_RK_Wheat.LoadFromFile(path+'RK_Wheat.bmp');
  FPic_RK_Ore    := TBitmap.Create; FPic_RK_Ore.LoadFromFile(path+'RK_Ore.bmp');
  FPic_PK_None   := TBitmap.Create; FPic_PK_None.LoadFromFile(path+'PK_None.bmp');

  S_All.Canvas.Pen.Width := 15;
end;

procedure TTTiatai_FieldForm.FormDestroy(Sender: TObject);
begin
  FPic_RK_Wood.Free;
  FPic_RK_Dirt.Free;
  FPic_RK_Sheeps.Free;
  FPic_RK_Wheat.Free;
  FPic_RK_Ore.Free;
  FPic_PK_None.Free;
end;

procedure TTTiatai_FieldForm.UpdateFieldImage(AImage: TImage; AField: TField;
  AHasThief: boolean);
begin
  case AField.Kind of
    RK_Wood   : AImage.Canvas.Draw(0,0,FPic_RK_Wood);
    RK_Dirt   : AImage.Canvas.Draw(0,0,FPic_RK_Dirt);
    RK_Sheeps : AImage.Canvas.Draw(0,0,FPic_RK_Sheeps);
    RK_Wheat  : AImage.Canvas.Draw(0,0,FPic_RK_Wheat);
    RK_Ore    : AImage.Canvas.Draw(0,0,FPic_RK_Ore);
    PK_None   : AImage.Canvas.Draw(0,0,FPic_PK_None);
  end;
  AImage.Canvas.Font.Size :=14;
  AImage.Canvas.TextOut(40,40,inttostr(AField.dice));
  AImage.Canvas.Font.Size :=9;
  AImage.Canvas.TextOut(40,20,'('+inttostr(AField.x)+'|'+inttostr(AField.y)+')');
  if AHasThief then
  begin
    AImage.Canvas.Pen.Color := clRed;
    AImage.Canvas.Pen.Width := 3;
    AImage.Canvas.MoveTo(0,0);
    AImage.Canvas.LineTo(100,100);
    AImage.Canvas.MoveTo(0,100);
    AImage.Canvas.LineTo(100,0);
  end;
end;

procedure TTTiatai_FieldForm.UpdateCities;
var
  i: integer;
  City: TCity;
  Image: TImage;
begin
  for i:= 0 to GlobalSystemManager.IO.Data.Map.CityCount-1 do
  begin
    City := GlobalSystemManager.IO.Data.Map.CityByID[i];
    Image := self.FindComponent('C_'+inttostr(City.X)+'_'+inttostr(City.Y)) as TImage;

    image.Canvas.Pen.Color := PlayerColorToColor(city.Owner.Color);
    image.Canvas.Brush.Color := PlayerColorToColor(city.Owner.Color);
    image.Canvas.Brush.Style := bsSolid;
    image.Canvas.Rectangle(0,0,25,25);
    if City.isExtended then
    begin
      image.Canvas.Font.Color := clgreen;
      image.Canvas.Brush.Style := bsClear;
      image.Canvas.TextOut(5,8,'E');
    end;
  end;
end;

procedure TTTiatai_FieldForm.UpdateStreets;
var
  i: integer;
  a,b: TPoint;
  street: TStreet;
  function getPoint(APos: TCityPosition): TPoint;
  var
    img: TImage;
  begin
    Img := self.FindComponent('C_'+inttostr(APos.x)+'_'+inttostr(APos.Y)) as TImage;
    result.x := Img.Left + (img.Width div 2);
    result.y := img.Top + (img.Height div 2);
  end;
begin
  S_All.Canvas.Clear;
  for i:= 0 to GlobalSystemManager.IO.Data.Map.StreetCount-1 do
  begin
    Street := GlobalSystemManager.IO.Data.Map.StreetByID[i];

    a := getPoint(Street.Position[0]);
    b := getPoint(Street.Position[1]);

    S_All.Canvas.Pen.Color := PlayerColorToColor(Street.Owner.Color);
    S_All.Canvas.Line(a,b);
  end;
end;

procedure TTTiatai_FieldForm.UpdateInfos;
begin
  if not visible then exit;
  UpdatePlayField;
  UpdateCities;
  UpdateStreets;
  UpdateHarbors;
end;

procedure TTTiatai_FieldForm.UpdatePlayField;
var
  i: integer;
  Field: TField;
  Image: TImage;
begin
  for i:= 0 to GlobalSystemManager.IO.Data.Map.FieldCount -1 do
  begin
    Field := GlobalSystemManager.IO.Data.Map.FieldsByID[i];
    Image := self.FindComponent('F_'+inttostr(Field.X)+'_'+inttostr(Field.Y)) as TImage;
    UpdateFieldImage(Image,Field,Field.position=GlobalSystemManager.IO.Data.Map.ThiefPositionAsFieldPosition);
  end;
end;

procedure TTTiatai_FieldForm.UpdateHarbors;
  function HarborKindToStr(AHarborKind: TResKind): string;
  begin
    result := 'not detected';
    case aHarborKind of
      RK_Wood:
         begin
           result := 'Wood'+' 2:1';
         end;
      RK_Dirt:
         begin
           result := 'Dirt'+' 2:1';
         end;
      RK_Sheeps:
         begin
           result := 'Sheeps'+' 2:1';
         end;
      RK_Wheat:
         begin
           result := 'Wheat'+' 2:1';
         end;
      RK_Ore:
         begin
           result := 'Ore'+'2:1';
         end;
      PK_None:
         begin
           result := 'Anything';
         end;
    end;
  end;

  function GetPoint(AHarborID: integer): TPoint;
  begin
    result.x := 10;
    result.y := 10;
    case aHarborID of
      0:
         begin
           result.x := 300;
           result.y := 15;
         end;
      1:
         begin
           result.x := 512;
           result.y := 113;
         end;
      2:
         begin
           result.x := 580;
           result.y := 273;
         end;
      3:
         begin
           result.x := 590;
           result.y := 425;
         end;
      4:
         begin
           result.x := 403;
           result.y := 529;
         end;
      5:
         begin
           result.x := 185;
           result.y := 529;
         end;
      6:
         begin
           result.x := 10;
           result.y := 425;
         end;
      7:
         begin
           result.x := 10;
           result.y := 273;
         end;
      8:
         begin
           result.x := 40;
           result.y := 113;
         end;
    end;
  end;

var
  i: integer;
begin
  S_All.Canvas.Font.Color := clBlack;
  S_All.Canvas.Clear;
  for i := 0 to 8 do
  begin
    S_All.Canvas.TextOut(GetPoint(i).x,GetPoint(i).y,
           HarborKindToStr(GlobalSystemmanager.IO.Data.map.Harbor[i].resource));
  end;
end;

end.

